package com.hualan.sfzy.controller.ssh;

import com.hualan.sfzy.model.entity.Answer;
import com.hualan.sfzy.model.entity.PsychologyTest;
import com.hualan.sfzy.model.entity.PsychologyTestTitle;
import com.hualan.sfzy.service.impl.PsychologyTestTitleServiceImpl;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/psychologyTestTitle")
public class PsychologyTestTitleController {
    @Autowired
    private PsychologyTestTitleServiceImpl Service;
    /*添加*/
    @PostMapping("/add")
    public Result add(@RequestBody PsychologyTestTitle entity) {
        int i = Service.add(entity);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }
    /*编辑*/
    @PutMapping("/edit")
    public Result edit(@RequestBody PsychologyTestTitle entity) {
        int i = Service.edit(entity);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*删除*/
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id) {
        int i = Service.remove(id);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*批量删除*/
    @DeleteMapping("/delBatch")
    public Result delBatch(@RequestParam("ids") Integer[] ids) {
        // 参数校验
        if (ids == null || ids.length == 0) {
            return Result.error("请提供有效的删除 ID 列表");
        }
        int i = Service.removeBatch(ids);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error("未删除任何记录");
    }

    /*查询所有*/
    @GetMapping("/find")
    public Result find() {
        List<PsychologyTestTitle> list = Service.find();
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }
    /*分页查询所有*/
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                           PsychologyTestTitle entity) {
        Map<String, Object> map = Service.findPage(pageNum, pageSize, entity);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }

    /*根据id查询单个*/
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error("ID参数不合法");
        }
        PsychologyTestTitle psychologyTestTitle = Service.findById(id);
        if (psychologyTestTitle != null) {
            return Result.success(psychologyTestTitle);
        }
        return Result.error("未找到ID为" + id + "的记录");
    }

    /*根据id查询单个(多表)*/
    @GetMapping("/findByIdDuo/{id}")
    public Result findByIdDuo(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error("ID参数不合法");
        }
        PsychologyTestTitle psychologyTestTitle = Service.findByIdDuo(id);
        if (psychologyTestTitle != null) {
            return Result.success(psychologyTestTitle);
        }
        return Result.error("未找到ID为" + id + "的多表记录");
    }

    /*根据title多表(模糊)*/
    @GetMapping("/findByCondition")
    public Result findByCondition(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                  PsychologyTestTitle entity
                                 ) {
        Map<String, Object> map = Service.findByCondition(pageNum, pageSize, entity);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }

    /*查询所有title*/
    @GetMapping("/findAllTitle")
    public Result findAllTitle() {
        List<Map<String, Object>> List= Service.selectAllTitle();
        if (List.size() > 0) {
            return Result.success(List);
        }
        return Result.error();
    }

    /*多表添加*/
    @PostMapping("/addDuo")
    public Result addDuo(@RequestBody PsychologyTestTitle entity) {
        int i = Service.addDuo(entity);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*多表编辑*/
    @PutMapping("/editDuo")
    public Result editDuo(@RequestBody PsychologyTestTitle entity) {
        int i = Service.editDuo(entity);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }
    /*根据testid查找对应的题目和选项*/
    @GetMapping("/selectByTestId/{testId}")
    public Result selectByTestId(@PathVariable("testId") Integer testId) {
        List<PsychologyTestTitle> list = Service.selectByTestId(testId);
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }


}
